package com.spring.aop1.service02;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * ClassName: TransactionAspect
 * Package: cw.study.spring.aspect
 * Description:
 * 事务切面类
 *  @Author: lxh
 *  @name: TransactionAspect
 *  Date: 2024/6/22  22:40
 */
@Component
@Aspect
public class TransactionAspect {

    // cw.study.spring.service 包下的所有类的所有方法
    @Around("execution(* com.spring.aop1.service02..*(..))")
    public void aroundAdvice(ProceedingJoinPoint joinPoint) {
        try {
            // 前环绕，开启事务
            System.out.println("开启事务...");
            // 执行目标
            joinPoint.proceed();
            // 后环绕
            // 完成事务，提交事务
            System.out.println("提交事务...");
        } catch (Throwable e) {
            // 发生异常，事务回滚
            System.out.println("回滚事务");
        }
    }

}
